<?php
    
	include_once "include/constants.php";
	
    if (!isset($_GET['movieid']) || !isset($_GET['friends']) || !isset($_GET['date'])) {
        die("Error");
    }
    
    $movieid = $_GET['movieid'];
    $friends = $_GET['friends'];
    $date = $_GET['date'];

    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare("insert into movienights (movieid, date)
                                values ($movieid, STR_TO_DATE('$date','%d-%m-%Y'))");
        $stmt->execute();
        
        $movienightid = $conn->lastInsertId();
        
        $friends = explode("$", $friends);
        foreach ($friends as $friend) {
            if (is_numeric($friend)) {
                $stmt = $conn->prepare("insert into movienightstousers (movienightid, userid) values ($movienightid, $friend)");
                $stmt->execute();
				
				$stmt = $conn->prepare("insert into notifications (userid, active, movienightid) values ($friend, 1, $movienightid)");
                $stmt->execute();
            }
        }
        
        echo "Success";
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>
